using System;
using System.Collections.Generic;
using System.Text;
using Umdf.ComInterop;
using System.IO;

namespace Umdf.SideShow.Events
{
    [EventId(SideShowEvents.SIDESHOW_APPLICATION_EVENT)]
    public abstract class ApplicationEvent : SideShowEvent
    {
        protected const uint ApplicationEventSize = 44;
        private Guid _applicationId;
        private Guid _endpointId;
        private uint _eventId;

        protected ApplicationEvent()
        {
        }

        public ApplicationEvent(Guid applicationId, Guid endpointId, int eventId) : this(applicationId, endpointId, (uint)eventId)
        {
           
        }

        internal ApplicationEvent(Guid applicationId, Guid endpointId, uint eventId)
        {
            _applicationId = applicationId;
            _endpointId = endpointId;
            _eventId = eventId;
        }

        public Guid ApplicationId
        {
            get { return _applicationId; }
        }

        public Guid EndpointId
        {
            get { return _endpointId; }
        }

        public int EventId
        {
            get { return (int) _eventId; }
        }

        protected abstract int EventDataSize { get; }

        protected override void WriteEventData(System.IO.Stream s)
        {
            BinaryWriter writer = new BinaryWriter(s);
            uint size = (uint)EventDataSize;
            writer.Write(size + 44);
            writer.Write(_applicationId.ToByteArray());
            writer.Write(_endpointId.ToByteArray());
            writer.Write(_eventId);
            writer.Write(size);
        }

        protected internal override void DeserializeObject(BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _applicationId = new Guid(reader.ReadBytes(16));
            _endpointId = new Guid(reader.ReadBytes(16));
            _eventId = reader.ReadUInt32();
        }

        protected internal override void SerializeObject(BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write(_applicationId.ToByteArray());
            writer.Write(_endpointId.ToByteArray());
            writer.Write(_eventId);
        }
    }
}
